# ------------------------------------------------------------------------------------------------
### Generate Table 10: Effects for municipalities <300,000 inhabitants
# ------------------------------------------------------------------------------------------------

# Load data 
load("../data/results/sdid_results.Rda")

# Filter and prepare data - exclude cities >300k
filtered_results <- filter_sdid_results(
  sdid_results,
  outcome_filter = c("Overall Turnout", "Party Turnout"),
  party_filter = c("Opposition", "Overall Turnout", "Law and Justice (PiS)"),
  no300k_sample_filter = "No cities >300k"
) %>%
  mutate(
    outcome = case_when(
      outcome == "Party Turnout" & party == "Opposition" ~ "Opposition Turnout",
      outcome == "Party Turnout" & party == "Law and Justice (PiS)" ~ "Government Turnout",
      TRUE ~ outcome
    ),
    outcome = factor(outcome, levels = c("Overall Turnout", "Opposition Turnout", "Government Turnout")),
    sample = ifelse(sample == "< 50km Sample", "$<$ 50km Sample", sample),
    sample = factor(sample, levels = c("$<$ 50km Sample", "Full Sample"))
  ) %>%
  arrange(outcome)

# Generate and save table
latex_table <- create_latex_table(
  filtered_results,
  group_var = "outcome",
  row_var = "sample",
  caption = "Synthetic Difference-in-Differences Estimates Excluding Cities >300k",
  label = "tab:sdid_no_big_cities",
  col_names = c("Outcome", "Sample", "Estimate (SE)", "N")
)

save_table(latex_table, "../output/tables/table10.txt")
